home *** CD-ROM | disk | FTP | other *** search
/ MacFormat UK 179 / MF_UK_179_1.iso / DiscContents / In the mag / Widgets / CheckDomain 1.1 / CheckDomain / CheckDomain.wdgt / CheckDomain.js < prev    next >
Encoding:
JavaScript  |  2006-09-20  |  12.7 KB  |  455 lines

  1. // --- VARS ---
  2.  
  3. var widgetVersion = "1.1";
  4. var widgetVersionPage = "http://www.bewidget.com/widget_version.php?widget=CheckDomain";
  5.  
  6. var registrar = 0;
  7.  
  8. var registered = 0; //0:visit | 1:whois
  9.  
  10. var okay = true;
  11. var err = "";
  12.  
  13. var registrars = new Array("GoDaddy", "Dotster", "1&1", "NetFirms", "BuyDomains", "iPower");
  14.  
  15. // --- UPDATES ---
  16.  
  17. function hideUpdate(){
  18.     document.getElementById("update").style.display = "none";
  19. }
  20.  
  21. function swImg(elt, newsrc){
  22.     elt.src = newsrc;
  23. }
  24.  
  25. function checkForUpdates(){
  26.     var req = new XMLHttpRequest();
  27.     req.onreadystatechange = function(){
  28.         if (req.readyState == 4) {
  29.             if (req.status == 200) {
  30.                 
  31.                 string = req.responseText;
  32.                 if(string != widgetVersion && string != ""){
  33.                     document.getElementById("update").style.display = "block";
  34.                 }else{
  35.                     return false;
  36.                 }
  37.                 
  38.             }
  39.         }
  40.     }
  41.     
  42.     req.open("GET", widgetVersionPage, true);
  43.     req.send("");
  44. }
  45.  
  46.  
  47. function setup(){
  48.     
  49.     if(loadPref('registrar') != null){
  50.         registrar = loadPref('registrar');
  51.     }
  52.     
  53.     if(loadPref('registered') != null){
  54.         registered = loadPref('registered');
  55.     }
  56.     
  57.     document.getElementById("avail").selectedIndex = registrar;
  58.     document.getElementById("unavail").selectedIndex = registered;
  59.     checkForUpdates();
  60. }
  61.  
  62. function availChange(){
  63.     registrar = document.getElementById("avail").options[document.getElementById("avail").selectedIndex].value;
  64.     savePref("registrar", registrar);
  65. }
  66.  
  67. function unavailChange(){
  68.     registered = document.getElementById("unavail").options[document.getElementById("unavail").selectedIndex].value;
  69.     savePref("registered", registered);
  70. }
  71.  
  72. function checkDomain(){
  73.     restore();
  74.     okay = true;
  75.     domain = document.getElementById("domain_field").value;
  76.     
  77.     domain = trim(domain);
  78.     document.getElementById("domain_field").value = domain;
  79.         
  80.     domain_validity = checkDV(domain);
  81.     if(domain_validity == false){
  82.         restore();
  83.         okay = false;
  84.         document.getElementById("status").innerHTML=err;
  85.         return;
  86.     }
  87.     
  88.     regex = /[\.]+/ig
  89.     if(domain.match(regex)){
  90.         splited = domain.split(".");
  91.         domain = splited[0];
  92.         document.getElementById("domain_field").value = domain;
  93.     }
  94.     
  95.     document.getElementById("status").innerHTML="loading...";
  96.     document.getElementById("com").src="images/com_off.png";
  97.     document.getElementById("net").src="images/net_off.png";
  98.     document.getElementById("org").src="images/org_off.png";
  99.     
  100.     //var url ="http://order.sbs.yahoo.com/ds/DomainSearchResults?d="+domain+"&.p=&m=dom&.src=sbs";
  101.     var url = "https://secure.registerapi.com/order/register/check.php?siteid=42566&DomainName="+domain+"&comtld=.com&nettld=.net&orgtld=.org&x=5&y=6";
  102.     
  103.     
  104.     req = new XMLHttpRequest();
  105.     req.onreadystatechange = function(){
  106.         
  107.         if (req.readyState == 4) {
  108.             // only if "OK"
  109.             if (req.status == 200) {
  110.                 
  111.                 string = req.responseText;
  112.                 
  113.                 
  114.                 entries = /<span\sclass=\"(available|notavailable)\">[^<]+/ig
  115.                 
  116.                 if(string.match(entries)){
  117.                     matches = string.match(entries);
  118.                     alert(matches);
  119.                     alert(matches.length);
  120.                     
  121.                     avail = "available";
  122.                     notavail = "notavailable";
  123.                     
  124.                     var data = new Array();
  125.                     
  126.                     for(i=0;i<matches.length;i++){
  127.                         if(matches[i].match(notavail)){
  128.                             data[i] = 1;
  129.                         }else if(matches[i].match(avail)){
  130.                             data[i] = 0;
  131.                         }
  132.                     }
  133.                     
  134.                 }else{
  135.                     document.getElementById("status").innerHTML = "an error ocurred";
  136.                     return;
  137.                 }
  138.                                 
  139.                 if(okay == true){
  140.                     if(data[0] == 0){
  141.                         thelink = getLink(domain, "com");
  142.                         document.getElementById("comlink").innerHTML="<a href=\"javascript:widget.openURL('"+thelink+"');\" title=\"register "+domain+".com\"><img id=\"com\" src=\"images/com_available.png\"></a>";
  143.                     }else{
  144.                         thelink = getULink(domain, "com");
  145.                         document.getElementById("comlink").innerHTML="<a href=\"javascript:widget.openURL('"+thelink+"');\" title=\"visit "+domain+".com\"><img id=\"com\" src=\"images/com_taken.png\"></a>";
  146.                     }
  147.                     
  148.                     if(data[1] == 0){
  149.                         thelink = getLink(domain, "net");
  150.                         document.getElementById("netlink").innerHTML="<a href=\"javascript:widget.openURL('"+thelink+"');\" title=\"register "+domain+".net\"><img id=\"net\" src=\"images/net_available.png\"></a>";
  151.                     }else{
  152.                         thelink = getULink(domain, "net");
  153.                         document.getElementById("netlink").innerHTML="<a href=\"javascript:widget.openURL('"+thelink+"');\" title=\"visit "+domain+".net\"><img id=\"net\" src=\"images/net_taken.png\"></a>";
  154.                     }
  155.                     
  156.                     if(data[2] == 0){
  157.                         thelink = getLink(domain, "org");
  158.                         document.getElementById("orglink").innerHTML="<a href=\"javascript:widget.openURL('"+thelink+"');\" title=\"register "+domain+".org\"><img id=\"org\" src=\"images/org_available.png\"></a>";
  159.                     }else{
  160.                         thelink = getULink(domain, "org");
  161.                         document.getElementById("orglink").innerHTML="<a href=\"javascript:widget.openURL('"+thelink+"');\" title=\"visit "+domain+".org\"><img id=\"org\" src=\"images/org_taken.png\"></a>";
  162.                     }
  163.                     
  164.                     document.getElementById("status").innerHTML="";
  165.                 }
  166.             } else {
  167.                 document.getElementById("status").innerHTML ="error: "+req.status;
  168.             }
  169.         }
  170.     }
  171.     
  172.     req.open("GET", url, true);
  173.     req.send("");
  174.     
  175. }
  176.  
  177. function restore(){
  178.     
  179.     document.getElementById("status").innerHTML="";
  180.     document.getElementById("comlink").innerHTML="<a href=\"\" title=\"\"><img id=\"com\" src=\"images/com_off.png\"></a>";
  181.     document.getElementById("netlink").innerHTML="<a href=\"\" title=\"\"><img id=\"net\" src=\"images/net_off.png\"></a>";
  182.     document.getElementById("orglink").innerHTML="<a href=\"\" title=\"\"><img id=\"org\" src=\"images/org_off.png\"></a>";
  183.     
  184. }
  185.  
  186.  
  187. function checkDV(domain){
  188.     
  189.     if(domain.length == 0){
  190.         err = "";
  191.         return false;    
  192.     }
  193.     
  194.     regex = /[a-zA-Z0-9-\.]/g
  195.     if(domain.match(regex)){
  196.         matches = domain.match(regex);
  197.         if(matches.length != domain.length){
  198.             err = "syntax error";
  199.             return false;
  200.         }
  201.     }else{
  202.         return false;    
  203.     }
  204.     
  205.     regex = /[\.\-]+$/g
  206.     if(domain.match(regex)){
  207.         err = "syntax error";
  208.         return false;
  209.     }
  210.     
  211.     return true;
  212.     
  213. }
  214.  
  215.  
  216. function trim(string){
  217.     
  218.     string = string.replace(/\s+/g, "");
  219.     return string;
  220.     
  221. }
  222.  
  223.  
  224. function getLink(domain, tld){
  225.     
  226.     if(registrar == 1){
  227.         return "http://www.kqzyfj.com/interactive?DomainName="+domain+"."+tld+"&siteid=4798&submit=GO&pid=1960393&url=https://secure.registerapi.com/DDS/controller.php&aid=5463237";
  228.     }
  229.     
  230.     if(registrar == 3){
  231.         return "http://www.kqzyfj.com/interactive?oldstage=ext&newstage=modify&postrun=1&verifypost=1&domainsignup=1&suhosting=hostingbasic&domainproduct=domain1yr&domainname="+domain+"&tld="+tld+"&submit=submit&pid=1960393&url=https://secure.netfirms.com/signup/stage/summary/hostingplan/basic/domaintype/1yearregistration&aid=10381702";
  232.     }
  233.     
  234.     if(registrar == 2){
  235.         return "http://www.tkqlhce.com/interactive?__currentindex[domaincheck]=0&__sendingdata=1&__pageflow=Order&multicheck.Domain="+domain+"&multicheck.Tlds="+tld+"&pid=1960393&url=http://order.1and1.com/xml/order/domaincheck?ac=OM.US.US469K02463T2103a&aid=10376103";
  236.     }
  237.     
  238.     if(registrar == 0){
  239.         return "http://www.kqzyfj.com/click?AID=10390987&PID=1960393&url=http://www.godaddy.com/gdshop/registrar/search.asp?isc=0000000000&domainToCheck="+domain+"&tld=."+tld+"&checkAvail=1&submit=submit";
  240.     }
  241.     
  242.     if(registrar == 4){
  243.         return "http://www.kqzyfj.com/click?AID=10429673&PID=1960393&url=http://www.buydomains.com/home.jsp?referrer_id=4052";
  244.     }
  245.     
  246.     if(registrar == 5){
  247.         return "http://www.anrdoezrs.net/click?AID=10392398&PID=1960393&url=http://www.ipower.com/";
  248.     }
  249. }
  250.  
  251.  
  252. function getULink(domain, tld){
  253.     
  254.     if(registered == 0){
  255.         return "http://www."+domain+"."+tld;
  256.     }
  257.     
  258.     if(registered == 1){
  259.         return "http://www.checkdomain.com/cgi-bin/checkdomain.pl?domain="+domain+"."+tld;
  260.     }
  261.     
  262. }
  263.  
  264.  
  265. /*********************************/
  266. // HIDING AND SHOWING PREFERENCES
  267. /*********************************/
  268.  
  269. // showPrefs() is called when the preferences flipper is clicked upon.  It freezes the front of the widget,
  270. // hides the front div, unhides the back div, and then flips the widget over.
  271. var hidingprefs=false;
  272.  
  273. function showPrefs()
  274. {
  275.     window.resizeTo(258,138);
  276.     var front = document.getElementById("front");
  277.     var back = document.getElementById("back");
  278.         
  279.     if (window.widget)
  280.         widget.prepareForTransition("ToBack");        // freezes the widget so that you can change it without the user noticing
  281.     
  282.     front.style.display="none";        // hide the front
  283.     back.style.display="block";        // show the back
  284.     
  285.     if (window.widget)
  286.         setTimeout ('widget.performTransition();', 0);        // and flip the widget over    
  287.  
  288.     document.getElementById('fliprollie').style.display = 'none';  // clean up the front side - hide the circle behind the info button
  289.  
  290. }
  291.  
  292.  
  293. // hidePrefs() is called by the done button on the back side of the widget.  It performs the opposite transition
  294. // as showPrefs() does.
  295.  
  296. function hidePrefs()
  297. {
  298.     
  299.     var front = document.getElementById("front");
  300.     var back = document.getElementById("back");
  301.         
  302.     if (window.widget)
  303.         widget.prepareForTransition("ToFront");        // freezes the widget and prepares it for the flip back to the front
  304.     
  305.     back.style.display="none";            // hide the back
  306.     front.style.display="block";        // show the front
  307.     
  308.     window.resizeTo(258,108);
  309.     
  310.     if (window.widget)
  311.         setTimeout ('widget.performTransition();', 0);        // and flip the widget back to the front
  312.     hidingprefs=true;
  313. }
  314.  
  315.  
  316. // PREFERENCE BUTTON ANIMATION (- the pref flipper fade in/out)
  317.  
  318. var flipShown = false;        // a flag used to signify if the flipper is currently shown or not.
  319.  
  320.  
  321. // A structure that holds information that is needed for the animation to run.
  322. var animation = {duration:0, starttime:0, to:1.0, now:0.0, from:0.0, firstElement:null, timer:null};
  323.  
  324.  
  325. // mousemove() is the event handle assigned to the onmousemove property on the front div of the widget. 
  326. // It is triggered whenever a mouse is moved within the bounds of your widget.  It prepares the
  327. // preference flipper fade and then calls animate() to performs the animation.
  328.  
  329. function mousemove (event)
  330. {
  331.     if (!flipShown)            // if the preferences flipper is not already showing...
  332.     {
  333.         if (animation.timer != null)            // reset the animation timer value, in case a value was left behind
  334.         {
  335.             clearInterval (animation.timer);
  336.             animation.timer  = null;
  337.         }
  338.         
  339.         var starttime = (new Date).getTime() - 13;         // set it back one frame
  340.         
  341.         animation.duration = 500;                                                // animation time, in ms
  342.         animation.starttime = starttime;                                        // specify the start time
  343.         animation.firstElement = document.getElementById ('flip');                // specify the element to fade
  344.         animation.timer = setInterval ("animate();", 13);                        // set the animation function
  345.         animation.from = animation.now;                                            // beginning opacity (not ness. 0)
  346.         animation.to = 1.0;                                                        // final opacity
  347.         animate();                                                                // begin animation
  348.         flipShown = true;                                                        // mark the flipper as animated
  349.     }
  350. }
  351.  
  352. // mouseexit() is the opposite of mousemove() in that it preps the preferences flipper
  353. // to disappear.  It adds the appropriate values to the animation data structure and sets the animation in motion.
  354.  
  355. function mouseexit (event)
  356. {
  357.     if (flipShown)
  358.     {
  359.         // fade in the flip widget
  360.         if (animation.timer != null)
  361.         {
  362.             clearInterval (animation.timer);
  363.             animation.timer  = null;
  364.         }
  365.         
  366.         var starttime = (new Date).getTime() - 13;
  367.         
  368.         animation.duration = 500;
  369.         animation.starttime = starttime;
  370.         animation.firstElement = document.getElementById ('flip');
  371.         animation.timer = setInterval ("animate();", 13);
  372.         animation.from = animation.now;
  373.         animation.to = 0.0;
  374.         animate();
  375.         flipShown = false;
  376.     }
  377. }
  378.  
  379.  
  380. // animate() performs the fade animation for the preferences flipper. It uses the opacity CSS property to simulate a fade.
  381.  
  382. function animate()
  383. {
  384.     var T;
  385.     var ease;
  386.     var time = (new Date).getTime();
  387.         
  388.     
  389.     T = limit_3(time-animation.starttime, 0, animation.duration);
  390.     
  391.     if (T >= animation.duration)
  392.     {
  393.         clearInterval (animation.timer);
  394.         animation.timer = null;
  395.         animation.now = animation.to;
  396.     }
  397.     else
  398.     {
  399.         ease = 0.5 - (0.5 * Math.cos(Math.PI * T / animation.duration));
  400.         animation.now = computeNextFloat (animation.from, animation.to, ease);
  401.     }
  402.     
  403.     animation.firstElement.style.opacity = animation.now;
  404.     if(hidingprefs==true){
  405.         hidingprefs=false;
  406.     }
  407. }
  408.  
  409.  
  410. // these functions are utilities used by animate()
  411.  
  412. function limit_3 (a, b, c)
  413. {
  414.     return a < b ? b : (a > c ? c : a);
  415. }
  416.  
  417. function computeNextFloat (from, to, ease)
  418. {
  419.     return from + (to - from) * ease;
  420. }
  421.  
  422. // these functions are called when the info button itself receives onmouseover and onmouseout events
  423.  
  424. function enterflip(event)
  425. {
  426.     document.getElementById('fliprollie').style.display = 'block';
  427. }
  428.  
  429. function exitflip(event)
  430. {
  431.     document.getElementById('fliprollie').style.display = 'none';
  432. }
  433.  
  434. // --- PREFS ---
  435. //
  436. // example:
  437. // savePref('widgetHeight", 540);
  438. // loadPref("widgetHeight");
  439.  
  440. function savePref(name, value){
  441.     if(window.widget){
  442.         widget.setPreferenceForKey(value, createKey(name));
  443.     }
  444. }
  445.  
  446. function createKey(keyName) {
  447.     return (widget.identifier + '-' + keyName);
  448. }
  449.  
  450. function loadPref(name){
  451.     if(window.widget){
  452.         value = widget.preferenceForKey(createKey(name));
  453.         return value;
  454.     }
  455. }